package com.system.runtai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.result.Result;
import com.system.runtai.entity.AssemblyBuilding;
import com.system.runtai.entity.EmergencyEquipment;
import com.system.runtai.service.AssemblyBuildingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 装配建筑 前端控制器
 * </p>
 *
 * @author runtai
 * @since 2024-04-05
 */
@Api(tags = "装配建筑")
@RestController
@RequestMapping("/runtai/assembly-building")
public class AssemblyBuildingController {

    @Autowired
    private AssemblyBuildingService assemblyBuildingService;

    @ApiOperation(value = "所有装配建筑")
    @GetMapping("/assemblyBuildingList")
    public Result<List<AssemblyBuilding>> assemblyBuildingList(){
        List<AssemblyBuilding> list = assemblyBuildingService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "装配建筑详情")
    @GetMapping("/assemblyBuilding/{id}")
    public Result<AssemblyBuilding> assemblyBuildingById(@PathVariable Integer id){
        AssemblyBuilding byId = assemblyBuildingService.getById(id);
        return Result.ok(byId);
    }

    @ApiOperation(value = "添加装配建筑")
    @PostMapping("/saveAssemblyBuilding")
    public Result<AssemblyBuilding> saveAssemblyBuilding(@RequestBody AssemblyBuilding assemblyBuilding){

        assemblyBuilding.setCreateTime(LocalDateTime.now());
        boolean save = assemblyBuildingService.save(assemblyBuilding);

        if (save){
            return Result.ok(assemblyBuilding).message("添加成功");
        }else {
            return Result.ok(assemblyBuilding).message("添加失败");
        }
    }

    @ApiOperation(value = "删除装配建筑")
    @PostMapping("delectAssemblyBuilding/{id}")
    public Result delectAssemblyBuilding(@PathVariable Integer id){

        boolean b = assemblyBuildingService.removeById(id);

        if (b){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }


    @ApiOperation(value = "修改装配建筑")
    @PostMapping("/updateAssemblyBuilding")
    public Result<AssemblyBuilding> updateAssemblyBuilding(@RequestBody AssemblyBuilding assemblyBuilding){

        assemblyBuilding.setUpdateTime(LocalDateTime.now());
        boolean update = assemblyBuildingService.updateById(assemblyBuilding);

        if (update){
            return Result.ok(assemblyBuilding).message("修改成功");
        }else {
            return Result.ok(assemblyBuilding).message("修改失败");
        }
    }

    @ApiOperation(value = "分页查询装配建筑")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<AssemblyBuilding>> indexdafg(@PathVariable Long page, @PathVariable Long limit, AssemblyBuilding assemblyBuilding){

        Page<AssemblyBuilding> recommendPage = new Page<>(page,limit);

        LambdaQueryWrapper<AssemblyBuilding> wrapper = new LambdaQueryWrapper<>();

        Integer id = assemblyBuilding.getId();

        if (id!=null){
            wrapper.eq(AssemblyBuilding::getId,id);
        }

        IPage<AssemblyBuilding> iPage = assemblyBuildingService.page(recommendPage,wrapper);

        return Result.ok(iPage);
    }


    @ApiOperation(value = "装配建筑名称筛选")
    @GetMapping("/filkte")
    public Result<IPage<AssemblyBuilding>> filtecarName(@RequestParam(required = false) String carName,@RequestParam(required = false) Integer limit,@RequestParam(required = false) Integer page){

        List<AssemblyBuilding> list = null;
        if (carName!=null) {
        LambdaQueryWrapper<AssemblyBuilding> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(AssemblyBuilding::getCarname,carName);
        list = assemblyBuildingService.list(wrapper);

            Page<AssemblyBuilding> recommendPage = new Page<>(page,limit);
            IPage<AssemblyBuilding> iPage = assemblyBuildingService.page(recommendPage,wrapper);
            return Result.ok(iPage);
        }else if (carName==null){
            list = assemblyBuildingService.list();
            Page<AssemblyBuilding> recommendPage = new Page<>(page,limit);
            IPage<AssemblyBuilding> iPage = assemblyBuildingService.page(recommendPage);
            return Result.ok(iPage);
        }
        Page<AssemblyBuilding> recommendPage = new Page<>(page,limit);
        IPage<AssemblyBuilding> iPage = null;
        return Result.fail(iPage);
    }

}

